import csv
import os
root_dir = os.getcwd()
pkg_path = os.path.join(root_dir, "update.log")
output_file = os.path.join(root_dir, "update.csv")

labels = ['pkg', "current_ver", "last_ver"]
pkgs_list = []
pkgs_list.append(labels)
with open(pkg_path, "r") as file:
    for line in file.readlines():
        if "Checking" in line:
            pkg_list = []
            pkgs_list.append(pkg_list)
            pkg = line.strip().split()[-1]
            pkg_list.append(pkg)
            print(pkg)
        elif "Current version" in line:
            current_version = line.strip().split()[-1]
            print(current_version)
            pkg_list.append(current_version)
        elif "Latest version" in line:
            latest_version = line.strip().split()[-1]
            print(latest_version)
            pkg_list.append(latest_version)
        
        else:
            continue
    pkgs_list.append(pkg_list)

print(pkgs_list)

labels = ['pkg', "current_ver", "last_ver"]

with open(output_file, "w", encoding="utf_8_sig", newline="") as file1:
    writer = csv.writer(file1)
    for one_data in pkgs_list:
        writer.writerow(one_data)
